package com.lad.transactional_propagation.service;

import com.lad.springboot_mybatis.entity.User;
import com.lad.springboot_mybatis.mapper.UserMapper;
import com.lad.transactional_propagation.annotation.MyTransaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author luheng
 * @create 2020-08-10 23:16
 */
@Service
public class PropagationService {
    @Autowired
    private UserMapper userMapper;

    @Transactional(propagation = Propagation.REQUIRED)
    public void insertUser1() {
        User user = new User(1, "zs", "123", "zhangsan");
        userMapper.insertUser(user);
        insertUser2();
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void insertUser2() {
        User user = new User(2, "zs2", "123", "zhangsan2");
        userMapper.insertUser(user);
        int i = 1 / 0;
    }
    @MyTransaction
    public void insertUser3() {
        User user = new User(3, "zs3", "123", "zhangsan3");
        userMapper.insertUser(user);
        int i = 1 / 0;
    }
}
